package com.tudouji.project.bill.controller;

import java.util.ArrayList;
import java.util.List;

import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.project.bill.domain.Transferapply;
import com.tudouji.project.bill.domain.Whinform;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.bill.domain.Whoutform;
import com.tudouji.project.bill.service.IWhoutformService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 出库单Controller
 * 
 * @author wf
 * @date 2021-01-12
 */
@RestController
@RequestMapping("/bill/whoutform")
public class WhoutformController extends BaseController
{
    @Autowired
    private IWhoutformService whoutformService;

    @Autowired
    private ISalespermissionService salespermissionService;

    /**
     * 查询出库单列表
     */
    @PreAuthorize("@ss.hasPermi('bill:whoutform:list')")
    @GetMapping("/list")
    public TableDataInfo list(Whoutform whoutform)
    {

        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Whoutform> list = new ArrayList<Whoutform>();
        // 查询用户是否有全局权限
        List<Salespermission> permissions = salespermissionService.getPermByScAndFunc(tuserId, "10", "4");
        int flag = 0;
        if (permissions.size() < 1) {// 没有全局权限
            // 渠道
            List<Salespermission> srs = salespermissionService.getPermByScAndFunc(tuserId, "10", "7");
            if (srs.size() > 0) {
                flag = 1;
                String[] srGuids = new String[srs.size()];
                for (int i = 0; i < srs.size(); i++) {
                    srGuids[i] = srs.get(i).getSrguid();
                }
                whoutform.setSrguids(srGuids);
                flag = 1;
            }
        } else {
            flag = 1;
        }
        if (flag == 1) {
            startPage();
            list = whoutformService.findWhoutformList(whoutform);
        }
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @PreAuthorize("@ss.hasPermi('bill:whoutform:export')")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Whoutform whoutform)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Whoutform> list = new ArrayList<Whoutform>();
        // 查询用户是否有全局权限
        List<Salespermission> permissions = salespermissionService.getPermByScAndFunc(tuserId, "10", "4");
        int flag = 0;
        if (permissions.size() < 1) {// 没有全局权限
            // 渠道
            List<Salespermission> srs = salespermissionService.getPermByScAndFunc(tuserId, "10", "7");
            if (srs.size() > 0) {
                flag = 1;
                String[] srGuids = new String[srs.size()];
                for (int i = 0; i < srs.size(); i++) {
                    srGuids[i] = srs.get(i).getSrguid();
                }
                whoutform.setSrguids(srGuids);
                flag = 1;
            }
        } else {
            flag = 1;
        }
        if (flag == 1) {
            //startPage();
            list = whoutformService.findWhoutformList(whoutform);
        }
        ExcelUtil<Whoutform> util = new ExcelUtil<Whoutform>(Whoutform.class);
        return util.exportExcel(list, "whoutform");
    }

    /**
     * 获取出库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('bill:whoutform:query')")
    @GetMapping(value = "/{autoid}")
    public AjaxResult getInfo(@PathVariable("autoid") Long autoid)
    {
        return AjaxResult.success(whoutformService.selectWhoutformById(autoid));
    }

    /**
     * 新增出库单
     */
    @PreAuthorize("@ss.hasPermi('bill:whoutform:add')")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Whoutform whoutform)
    {
        return toAjax(whoutformService.insertWhoutform(whoutform));
    }

    /**
     * 修改出库单
     */
    @PreAuthorize("@ss.hasPermi('bill:whoutform:edit')")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Whoutform whoutform)
    {
        return toAjax(whoutformService.updateWhoutform(whoutform));
    }

    /**
     * 删除出库单
     */
    @PreAuthorize("@ss.hasPermi('bill:whoutform:remove')")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{autoids}")
    public AjaxResult remove(@PathVariable Long[] autoids)
    {
        return toAjax(whoutformService.deleteWhoutformByIds(autoids));
    }

    @ApiOperation(value = "出库", httpMethod = "POST", notes = "出库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "items", value = "明细", required = true, dataType = "List", paramType = "body")
            }
    )
    //itemsOut传: itemguid:业务明细id
    //("whoutformIds"); // 出库单图片id
    //("overweightIds");// 过磅单图片id
    @Log(title = "出库", businessType = BusinessType.OTHER)
    @PostMapping("/doOutForm")
    public AjaxResult doOutForm(Long taskid, @RequestBody Whoutform entity, Long id, String opinion, String fileIds,String opttype) {
        return whoutformService.saveWhoutFormAndImg(id, entity, fileIds, opinion, taskid, opttype);
    }

    //入库单打印
    @GetMapping("/printWhout")
    public AjaxResult printWhout(Long autoid){
        return whoutformService.printWhout(autoid);
    }

    //入库单打印
    @GetMapping("/printWhoutHtml")
    public AjaxResult printWhoutHtml(Long autoid){
        return whoutformService.printWhoutHtml(autoid);
    }
}
